package com.sgb.kjwl.view.ui.adapter.self.logistics;

import android.content.Context;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.sgb.kjwl.BR;
import com.sgb.kjwl.R;
import com.sgb.kjwl.api.APIManager;
import com.sgb.kjwl.model.entity.self.BargainingEntity;
import com.sgb.kjwl.view.ui.activity.WebViewX5HostActivity;
import com.sgb.kjwl.view.ui.activity.self.logistics.BargainingActivity;
import com.swgk.core.base.model.entity.H5ReEntity;
import com.swgk.core.dialog.RoundCornerDialog;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.recyclerview.adapter.BaseRecycleAdapter;
import com.swgk.core.recyclerview.adapter.ItemViewHolder;
import com.swgk.core.util.MToast;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;


/*
 *  说明：我的议价 适配器（物流运输  货主）
 *  名称：md
 * */


public class BargainingAdater  extends BaseRecycleAdapter<BargainingEntity> {

    Context context;

    public BargainingAdater(Context context, List<BargainingEntity> datas) {
        super(context, R.layout.item_bargaining, datas);
        this.context=context;
    }



    @Override
    public void convert(ItemViewHolder holder, BargainingEntity bargainingEntity) {
        holder.setBinding(BR.viewModel, bargainingEntity);
        Log.e("BargainingAdater","0--1");
        TextView tv_agree=holder.itemView.findViewById(R.id.tv_agree);
        TextView tv_order=holder.itemView.findViewById(R.id.tv_oder);
        holder.itemView.findViewById(R.id.phone_number).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //点击弹框电话号码
             //   MToast.showToast(context, "正在升级中...");
                showDialog(bargainingEntity.getContrctsNumber());
            }
        });


        holder.itemView.findViewById(R.id.tV_order_mark).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //跳订单详情
            //    MToast.showToast(context, "正在升级中...");
                getH5Url(bargainingEntity.getOrderNo());
               // showDialog();
            }
        });
        holder.itemView.findViewById(R.id.tv_detail).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //跳议价历史
                getH5Url2(bargainingEntity.getUserNo(),bargainingEntity.getCarrierNo());
            }
        });
        holder.itemView.findViewById(R.id.ll_title).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //跳承运详情
                getH5Url3(bargainingEntity.getCarrierNo());

            }
        });
        if(!TextUtils.isEmpty(bargainingEntity.getOrderNo())){
            tv_order.setVisibility(View.VISIBLE);
            tv_agree.setText("已同意");
        }else{
            tv_order.setVisibility(View.GONE);
            tv_agree.setText("");
        }


    }


  /*  //临时加的，测试数据展示的
    @Override
    public int getItemCount() {
        return 5;
    }
  */



        private void showDialog(String phone) {
            Log.e("BargainingAdater","1");
            View view = View.inflate(context, R.layout.dialog_two_btn, null);
            final RoundCornerDialog roundCornerDialog = new RoundCornerDialog(context, 0, 0, view, R.style.RoundCornerDialog);
            roundCornerDialog.show();
            roundCornerDialog.setCanceledOnTouchOutside(false);// 设置点击屏幕Dialog不消失
            //roundCornerDialog.setOnKeyListener(keylistener);//设置点击返回键Dialog不消失

            TextView tv_message = view.findViewById(R.id.tv_message);
            TextView tv_title = view.findViewById(R.id.tv_title);
            TextView tv_logout_confirm = view.findViewById(R.id.tv_logout_confirm);
            TextView tv_logout_cancel = view.findViewById(R.id.tv_logout_cancel);
            tv_title.setText("确认呼叫");
            tv_title.setVisibility(View.VISIBLE);
            tv_message.setText(phone);
            tv_logout_confirm.setText("确定");
            //确定
            tv_logout_confirm.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
//                    Intent intent = new Intent(Intent.ACTION_DIAL);
//                    Uri data = Uri.parse("tel:" + "4000010051");
//                                        intent.setData(data);
//                                     startActivity(intent);
                }
            });
            //取消
            tv_logout_cancel.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    roundCornerDialog.dismiss();
                }
            });
        }
    // 详情       货主订单详情 参数  goodsOrderDetails      货源详情参数   sourceDetails     ownerDetails
    public void getH5Url(String id) {
        DialogHelper.showProgressDialog(context, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getSelfOwnerAPI().getUrlSetting("goodsOrderDetails").enqueue(new Callback<BaseEntity<H5ReEntity>>() {
            @Override
            public void onResponse(Call<BaseEntity<H5ReEntity>> call, Response<BaseEntity<H5ReEntity>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }
                Log.e("madong_test","3-------------"+response.body().getData().getUrl()+"       "+response.body().getData());
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    String url = response.body().getData().getUrl() + "?id=" + id;
                    Log.e("madong_test","4====="+url);
                    WebViewX5HostActivity.start(context, url);
                } else {
                    MToast.showToast(context, response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<H5ReEntity>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }
    // 议价历史详情
    public void getH5Url2(String userNo,String carrierNo) {
        DialogHelper.showProgressDialog(context, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getSelfOwnerAPI().getUrlSetting("bargainDetails").enqueue(new Callback<BaseEntity<H5ReEntity>>() {
            @Override
            public void onResponse(Call<BaseEntity<H5ReEntity>> call, Response<BaseEntity<H5ReEntity>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }
                Log.e("madong_test","3-------------"+response.body().getData().getUrl()+"       "+response.body().getData());
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    String url = response.body().getData().getUrl() + "?userNo=" + userNo+"&carrierNo="+carrierNo;
                    Log.e("madong_test","4====="+url);
                    WebViewX5HostActivity.start(context, url);
                } else {
                    MToast.showToast(context, response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<H5ReEntity>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }
    // 承运详情
    public void getH5Url3(String id) {
        DialogHelper.showProgressDialog(context, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getSelfOwnerAPI().getUrlSetting("carrierDetails").enqueue(new Callback<BaseEntity<H5ReEntity>>() {
            @Override
            public void onResponse(Call<BaseEntity<H5ReEntity>> call, Response<BaseEntity<H5ReEntity>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }
                Log.e("madong_test","3-------------"+response.body().getData().getUrl()+"       "+response.body().getData());
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    String url = response.body().getData().getUrl() + "?id=" + id+"&areaCode="+((BargainingActivity)context).viewModel.repertory.getAreaCode()+"&isMyMenu=true";
                    Log.e("madong_test","4====="+url);
                    WebViewX5HostActivity.start(context, url);
                } else {
                    MToast.showToast(context, response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<H5ReEntity>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }
    }




